class Solution
{
public:
    int maximizeTheProfit(int n, vector<vector<int>> &offers)
    {
        sort(offers.begin(), offers.end(), [](const vector<int> &a, const vector<int> &b)
             { return a[1] < b[1]; });

        map<int, int> dp;
        dp[-1] = 0;

        for (auto &offer : offers)
        {

            int start = offer[0];
            int end = offer[1];
            int score = offer[2];

            auto it = dp.lower_bound(start);
            auto it2 = dp.upper_bound(end);

            it--;
            it2--;
            dp[end] = max(it->second + score, it2->second);
        }

        return dp.rbegin()->second;
    }
};